package net.pessu.android.shoppoid.gui;

import net.pessu.android.shoppoid.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class ButtonListRemise extends ButtonList {

	private static final String TAG = "ButtonListRemise";
	
	private EditText input;
	private TextSolution solution;
	private double taux;
	private ButtonListLayout buttonLayout;
	private Context context;

	private boolean selected;

	public ButtonListRemise(Context context) {
		super(context);
		this.context = context;
	}

	public ButtonListRemise(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public ButtonListRemise(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
	}

	public void init(double taux, EditText inputParam, TextSolution solutionParam, ButtonListLayout remiseParam) {
		this.setOnClickListener(calculRemise(taux));
		this.selected = false;
		this.taux = taux;
		this.input = inputParam;
		this.solution = solutionParam;
		this.buttonLayout = remiseParam;
	}

	private OnClickListener calculRemise(final double taux) {
		return new OnClickListener() {
			public void onClick(View v) {

				double entre = 0;

				try {
					entre = Double.parseDouble(input.getText().toString().replace(',', '.'));
				} catch (NumberFormatException nfe) {
					Log.e(TAG, nfe.toString());
				}

				if (entre > 0) {
					buttonLayout.initializeButtons();
					entre = entre * taux;
					solution.setText(entre);
					select();
					InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
					inputManager.hideSoftInputFromWindow(input.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				}
			}
		};
	}
	
	public void select(){
		setSelected(true);
		setBackgroundResource(R.drawable.sacvioletsel);
	}
	
	public void unSelect(){
		setSelected(false);
		setBackgroundResource(R.drawable.sacviolet);
	}

	/**
	 * @return the selected
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * @param selected
	 *            the selected to set
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	/**
	 * @return the taux
	 */
	public double getTaux() {
		return taux;
	}

	/**
	 * @param taux
	 *            the taux to set
	 */
	public void setTaux(double taux) {
		this.taux = taux;
	}

}
